<template>
  <div class="home_bottom">
    <div class="operation_cont">
      <div class="oper_bj_top" @mouseenter="handleScroll" @mouseleave="removeHandleScroll">
        <div class="oper_bj_top_cont">
          <img src="@/assets/img/getLeft.png" alt="" @click="next('left')">
          <h2>{{ this.nextNum }}楼</h2>
          <img src="@/assets/img/getRight.png" alt="" @click="next('right')">
        </div>
        <h3>共{{ $store.state.maxTower }}楼</h3>
      </div>
      <div class="oper_bj_bottom">
        <h2 @click="scrollAlertLeft" v-if="this.arr.length !== 0">{{ this.oper_left_name }}单元</h2>
        <h2 @click="scrollAlertRight" v-if="this.arr.length !== 0">{{ this.oper_right_name }}</h2>
        <span class="topIcon1"></span>
        <span class="topIcon2"></span>
        <div class="oper_top oper_top_left" v-show="alertFlgLeft">
          <div class="close" @click="alertFlgLeft = !alertFlgLeft">
            <img src="@/assets/img/close.png" alt="img">
          </div>
          <el-tabs v-model="activeNameAlertLeft" @tab-click="handleClickAlertLeft" :tab-position="'left'"
            class="tabsAlert">
            <el-tab-pane v-for="(item, index) in arr" :key="index + ''" :label="item.unintNo + '单元'"></el-tab-pane>
          </el-tabs>
        </div>
        <div class="oper_top oper_top_right" v-show="alertFlgRight">
          <div class="close" @click="alertFlgRight = !alertFlgRight">
            <img src="@/assets/img/close.png" alt="img">
          </div>
          <el-tabs v-model="activeNameAlertRight" @tab-click="handleClickAlertRight" :tab-position="'left'"
            class="tabsAlert">
            <el-tab-pane v-for="(item, index) in infoArr" :key="index + ''" :label="item.houseNo">
            </el-tab-pane>
          </el-tabs>
        </div>
        <div class="oper_left">
          <el-tabs v-model="oper_left_name" type="card" @tab-click="handleClick_left">
            <el-tab-pane v-for="(item, index) in arr" :key="index + ''" :label="item.unintNo" :name="item.unintNo">
            </el-tab-pane>
            <!-- <el-tab-pane label="三单元" name="三单元"></el-tab-pane>
            <el-tab-pane label="四单元" name="四单元"></el-tab-pane> -->
          </el-tabs>
        </div>
        <div class="oper_right">
          <el-tabs v-model="oper_right_name" type="card" @tab-click="handleClick_right">
            <el-tab-pane v-for="(item, index) in infoArr" :key="index + ''" :label="item.houseNo" :name="item.houseNo">
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </div>
    <div class="operation_box" :style="changebj ? `background-image: url(${url1});` : `background-image: url(${url2});`"
      v-show="boxFlg">
      <div class="box_left">
        <!-- .split("").splice(2,0,"栋").join("") -->
        <h2>{{ $store.state.listData.infoS[0].buildNo < 10 ? $store.state.listData.infoS[0].address.slice(0, 1) + '栋' +
        $store.state.listData.infoS[0].address.slice(1) : $store.state.listData.infoS[0].address.slice(0, 2) + '栋' +
        $store.state.listData.infoS[0].address.slice(2) }}{{ this.oper_left_name }}单元{{ this.oper_right_name }}</h2>
            <!-- <h2>{{realArr}}</h2> -->
            <ul>
              <li><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.4);' : 'background-color: rgba(133, 29, 25, 0.5);'">户名</span><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.2); color: #17E8FF;' : 'background-color: rgba(133, 29, 25, 0.2); color: #ff9775;'">{{
                  realArr.clientName
                  }}</span>
              </li>
              <li><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.4);' : 'background-color: rgba(133, 29, 25, 0.5);'">户号</span><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.2); color: #17E8FF;' : 'background-color: rgba(133, 29, 25, 0.2); color: #ff9775;'">{{
                  realArr.wmCardNo
                  }}</span></li>
              <li><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.4);' : 'background-color: rgba(133, 29, 25, 0.5);'">联系方式</span><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.2); color: #17E8FF;' : 'background-color: rgba(133, 29, 25, 0.2); color: #ff9775;'">{{
                  realArr.phone
                  }}</span></li>
              <li><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.4);' : 'background-color: rgba(133, 29, 25, 0.5);'">用户类型</span><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.2); color: #17E8FF;' : 'background-color: rgba(133, 29, 25, 0.2); color: #ff9775;'">{{
                  realArr.clientTypeText
                  }}</span></li>
              <li><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.4);' : 'background-color: rgba(133, 29, 25, 0.5);'">水表类型</span><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.2); color: #17E8FF;' : 'background-color: rgba(133, 29, 25, 0.2); color: #ff9775;'">{{
                  realArr.managerTypeText
                  }}</span></li>
              <li><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.4);' : 'background-color: rgba(133, 29, 25, 0.5);'">联络员</span><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.2); color: #17E8FF;' : 'background-color: rgba(133, 29, 25, 0.2); color: #ff9775;'">{{
                  realArr.userStr
                  }}</span></li>
              <li v-show="!changebj"><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.4);' : 'background-color: rgba(133, 29, 25, 0.5);'">异常类型</span><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.2); color: #17E8FF;' : 'background-color: rgba(133, 29, 25, 0.2); color: #ff9775;'">{{
                  realArr.tplName
                  }}</span>
              </li>
              <li v-show="!changebj"><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.4);' : 'background-color: rgba(133, 29, 25, 0.5);'">处理方式</span><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.2); color: #17E8FF;' : 'background-color: rgba(133, 29, 25, 0.2); color: #ff9775;'">{{
                  realArr.dealTypeText
                  }}</span>
              </li>
              <li v-show="!changebj"><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.4);' : 'background-color: rgba(133, 29, 25, 0.5);'">异常智能分析</span><span
                  :style="changebj ? 'background-color: rgba(85, 136, 238, 0.2); color: #17E8FF;' : 'background-color: rgba(133, 29, 25, 0.2); color: #ff9775;'">{{
                  realArr.alarmContent
                  }}</span>
              </li>
            </ul>
      </div>
      <div class="box_line">
        <img v-if="changebj" src="@/assets/img/fgx3.png" alt="">
        <img v-else src="@/assets/img/fgx2.png" alt="">
      </div>
      <div class="box_right">
        <div class="oper_cont">
          <div class="oper_btns">
            <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
              <el-tab-pane label="水量" name="水量"></el-tab-pane>
              <el-tab-pane label="水压" name="水压"></el-tab-pane>
              <el-tab-pane label="水温" name="水温"></el-tab-pane>
              <el-tab-pane label="TDS" name="TDS"></el-tab-pane>
            </el-tabs>
          </div>
          <div class="oper_echarts"></div>
        </div>
      </div>
      <div class="close">
        <img v-if="changebj" @click="boxFlg = false" src="@/assets/img/close.png" alt="">
        <img v-else @click="boxFlg = false" src="@/assets/img/close2.png" alt="">
      </div>
    </div>
  </div>
</template>

<script>
import * as api from '@/assets/utils/api.js'
export default {
  props: {
    buildNo: String
  },
  data() {
    return {
      oper_left_name: 1,
      oper_right_name: '',
      activeNum: 3,
      activeName: '水量',
      myChart: {},
      company: '[m³]',
      // buildNo: '',
      // buildNo: '',
      floorNo: 4,
      arr: [],
      nextNum: 1,
      unitNum: 0,
      alertFlgLeft: false,
      alertFlgRight: false,
      activeNameAlertLeft: '',
      activeNameAlertRight: '',
      nextFlg: true,
      boxFlg: true,
      infoArr: {},
      leftSelect: 0,
      treeDataFlg: true,
      // 单元
      unintNo: '',
      // 楼室号
      houseNo: '',
      // 表身号
      meterNo: '',
      realArr: {},
      indexNum: 0,
      changebj: true,
      url1: new URL('../../assets/img/box.png', import.meta.url).href,
      url2: new URL('../../assets/img/redbj.png', import.meta.url).href
    }
  },
  created() {
    // this.getData()
  },
  mounted() {
    this.getData()
    //根据窗口变化自动调节图表大小
    window.addEventListener('resize', () => {
      this.myChart.resize()
    })
    // this.$nextTick(()=>{
    //   console.log(this.infoArr)
    // })
  },
  methods: {
    // changeBuildNo(id) {
    //   this.buildNo = id
    //   this.getData()
    // },
    async getData() {
      const res = await api.tree({ buildNo: this.buildNo, floorNo: this.nextNum })
      this.arr = res.data
      if (res.data[0]) {
        this.oper_left_name = res.data[0].infoS[0].unintNo
        this.oper_right_name = res.data[0].infoS[0].houseNo
        this.infoArr = res.data[this.leftSelect].infoS
        this.$nextTick(() => {
          setTimeout(() => {
            this.arr.forEach((item, index) => {
              document.querySelector(`.oper_top_left #tab-${index}`).setAttribute('title',item.unintNo + '单元')
            })
            this.infoArr.forEach((item, index) => {
              document.querySelector(`.oper_top_right #tab-${index}`).setAttribute('title',item.houseNo)
            })
          }, 1000)
        })
      } else {
        this.infoArr = {}
      }
      if (res.data.length == 0 && this.nextNum != this.$store.state.maxTower && this.nextNum != 1) {
        if(this.nextFlg){
          this.next('right')
        }else{
          this.next('left')
        }
      }
      this.getData2()
    },
    async getData2() {
      if (this.$store.state.nextFlg) this.getfloor2()
      this.$store.commit('nextFlgs', false)
      let res2 = [],waterArr = [],date = []
      // standardArr = []
      res2 = await api.realtimeMeter({ buildNo: this.buildNo, unintNo: this.oper_left_name, houseNo: this.oper_right_name, meterNo: this.meterNo, number: this.activeNum })
      if (res2.data[0]) {
        if (res2.data[0].alarmContent !== null) {
          this.changebj = false
          document.querySelector('.oper_btns').className = 'oper_btns operStyleChange'
          document.querySelector('.oper_left').className = 'oper_left redbj'
          document.querySelector('.oper_right').className = 'oper_right redbj'
          this.$emit('getNext', this.nextNum, 'red')
        } else {
          this.changebj = true
          document.querySelector('.oper_btns').className = 'oper_btns'
          document.querySelector('.oper_left').className = 'oper_left'
          document.querySelector('.oper_right').className = 'oper_right'
          this.$emit('getNext', this.nextNum, '')
        }
        this.realArr = res2.data[0]
        res2.data[0].infoS.forEach(item=>{
          waterArr.push(item.waterNum)
          date.push(item.time)
        })
      } else {
        this.realArr = {}
        // this.$emit('getNext', this.nextNum, '')
      }
      this.initChart(waterArr,date)
    },
    handleClick() {
      if (this.activeName == '水量') {
        this.company = '[m³/h]'
        this.activeNum = 3
      } else if (this.activeName == '水压') {
        this.company = '[Mpa]'
        this.activeNum = 6
      } else if (this.activeName == '水温') {
        this.company = '[℃]'
        this.activeNum = 7
      } else if (this.activeName == 'TDS') {
        this.company = '[PPM]'
        this.activeNum = 8
      }
      this.getData2()
    },
    // 弹窗选择单元
    handleClickAlertLeft() {
      document.querySelector('.oper_top_left .el-tabs__active-bar').style.top = this.activeNameAlertLeft * 0.31 + 'rem'
      this.oper_left_name = this.arr[this.activeNameAlertLeft].unintNo
      this.getData2()
      this.alertFlgLeft = false
    },
    // 弹窗选择楼室
    handleClickAlertRight() {
      document.querySelector('.oper_top_right .el-tabs__active-bar').style.top = this.activeNameAlertRight * 0.31 + 'rem'
      this.oper_right_name = this.arr[this.leftSelect].infoS[this.activeNameAlertRight].houseNo
      this.getData2()
      this.alertFlgRight = false
    },
    // 选择单元
    handleClick_left(tab) {
      this.leftSelect = Number(tab.index)
      this.getData2()
    },
    // 选择楼室
    handleClick_right() {
      this.boxFlg = true
      this.getData2()
    },
    // 对应的楼层异常用户显示红色
    getfloor() {
      this.$store.state.abnormalObj.forEach(item => {
        let newArr = item.split(',')
        if (this.buildNo == newArr[0] && this.nextNum == newArr[2]) {
          this.oper_left_name = newArr[1]
          this.oper_right_name = newArr[3]
          this.getData()
        }
      })
    },
    getfloor2() {
      this.$store.state.abnormalObj.forEach(item => {
        let newArr = item.split(',')
        if (this.buildNo == newArr[0]) {
          this.oper_left_name = newArr[1]
          this.nextNum = newArr[2]
          this.oper_right_name = newArr[3]
          this.getData()
        }
      })
    },
    next(fx) {
      if (fx == 'left') {
        this.nextFlg = false
        this.nextNum > 1 ? this.nextNum-- : this.nextNum = 1
      } else if (fx == 'right') {
        this.nextFlg = true
        this.nextNum < 7 ? this.nextNum++ : this.nextNum = 7
      }
      this.$emit('getNext', this.nextNum, '')
      this.getfloor()
      this.getData()
    },
    scrollFun(e) {
      let down = true
      down = e.wheelDelta ? e.wheelDelta < 0 : e.detail > 0
      if (down) {
        this.next('left')
      } else {
        this.next('right')
      }
      return false
    },
    // 鼠标移入
    handleScroll() {
      window.addEventListener('mousewheel', this.scrollFun, false)
    },
    // 鼠标移出
    removeHandleScroll() {
      window.removeEventListener('mousewheel', this.scrollFun, false)
    },
    scrollAlertLeft() {
      this.alertFlgLeft = !this.alertFlgLeft
    },
    scrollAlertRight() {
      this.alertFlgRight = !this.alertFlgRight
    },
    initChart(waterArr,date) {
      this.myChart = this.$echarts.init(document.querySelector('.oper_echarts'))
      this.myChart.setOption({
        color: ['#058cff', '#50fde7'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: 'cross' // 默认为直线，可选为：'line' | 'shadow'
          },
          extraCssText: 'background: #023881;border:none;color:#fff;opacity: 0.9;',
          formatter(params) {
            let res = []
            params.forEach((v)=>{
              res.push(`<span style="font-size: 0.12rem;">${v.seriesName}：<span style="color: #058cff">${v.value ? v.value : ''}</span></span>`)
            })
            return res.join('<br />')
            // return `
            // <span style="font-size: 0.12rem;">水量：<span style="color: #058cff">${params[0].value}</span></span> <br/>
            // <span style="font-size: 0.12rem;">标准值：<span style="color: #50fde7">${params[1].value}</span></span> <br/>
            // `
          }
        },
        grid: {
          width: '72%',
          // height: '55%',
          bottom: '30%'
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: date,
          axisLabel: {
            show: true,
            textStyle: {
              color: this.changebj ? '#50fde7' : '#ccc'
            }
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: this.changebj ? '#50fde7' : '#c82413'
            }
          },
        },
        legend: {
          data: [this.activeName, '标准值'],
          top: '4%',
          right: '5%',
          orient: 'vertical',
          itemHeight: 8,
          itemWidth: 12,
          textStyle: {
            fontSize: '10px',
            color: ['#058cff', '#50fde7']
          }
        },
        yAxis: [
          {
            name: this.company,
            nameTextStyle: {
              color: this.changebj ? '#17E8FF' : '#ccc'
            },
            type: 'value',
            position: 'left',
            alignTicks: true,
            splitLine: {
              show: true,
              lineStyle: {
                type: 'dashed',
                color: this.changebj ? '#5fa1ff' : '#c82413'
              }
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: this.changebj ? '#50fde7' : '#c82413'
              }
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: this.changebj ? '#50fde7' : '#ccc'
              }
            },
          }],
        dataZoom: [
          {
            type: 'slider',
            // show: true,
            height: 15,
            bottom: 10,
            start: 20,
            end: 80,
            textStyle: {
              color: '#50fde7'
            }
          }
        ],
        series: [
          {
            name: this.activeName,
            data: waterArr,
            type: 'line',
            showSymbol: false,
            areaStyle: {}
          },
          {
            name: '标准值',
            data: [],
            showSymbol: false,
            type: 'line'
          }
        ]
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.home_bottom {
  width: 100%;
  height: 38%;
  display: flex;
  flex-direction: column-reverse;
  z-index: 100;

  .operation_cont {
    width: 100%;
    height: 1.05rem;
    position: relative;
    top: 0.2rem;

    .oper_bj_top {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      top: 0;
      width: 23%;
      height: 100%;
      background: url(@/assets/img/yh.png) no-repeat center;
      background-size: 100% 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      z-index: 10;

      .oper_bj_top_cont {
        display: flex;
        align-items: center;
        margin: 0.1rem 0 0.01rem 0;

        h2 {
          font-family: 'Pang';
          color: #fff;
          font-size: 0.22rem;
          text-align: center;
        }

        img {
          width: 0.07rem;
          margin: 0 0.2rem;
          cursor: pointer;
        }

      }

      h3 {
        font-size: 0.12rem;
        color: #F9FAFA;
        opacity: .5;
      }

    }

    .oper_bj_bottom {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      height: 68%;
      background: url(@/assets/img/bottomBj.png) no-repeat center;
      background-size: 100% 100%;
      display: flex;
      justify-content: space-between;

      .oper_left {
        margin-left: 0.47rem;
      }

      .oper_right {
        margin-right: 0.47rem;
      }

      .oper_left,
      .oper_right {
        width: 31%;
        display: flex;
        justify-content: center;
        align-items: flex-end;
        // flex-direction: column;
        overflow-x: auto;
        position: relative;
        padding-top: 0.2rem;

      }

      h2 {
        color: #17E8FF;
        font-family: 'Pang';
        font-size: 0.18rem;
        position: absolute;
        z-index: 10;
        cursor: pointer;
        width: 17%;
        text-align: right;

        &:nth-child(1) {
          top: 10%;
          left: 6%;
        }

        &:nth-child(2) {
          top: 10%;
          right: 18%;
          width: 18%;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          text-align: right;
        }

        // span::before {
        //   content: '';
        //   width: 0;
        //   height: 0;
        //   border-left: 0.05rem solid transparent;
        //   border-right: 0.05rem solid transparent;
        //   border-bottom: 0.08rem solid #17E8FF;
        //   position: absolute;
        //   right: -0.16rem;
        //   top: 0.03rem;
        // }

        // span::after {
        //   content: '';
        //   width: 0.1rem;
        //   height: 0.01rem;
        //   position: absolute;
        //   right: -0.16rem;
        //   top: 0.12rem;
        //   background-color: #17E8FF;
        // }
      }

      .topIcon1 {
        top: 10%;
        left: 23.5%;
      }

      .topIcon2 {
        top: 10%;
        right: 18%;
      }

      .topIcon1,
      .topIcon2 {
        position: absolute;

        &::before {
          content: '';
          width: 0;
          height: 0;
          border-left: 0.05rem solid transparent;
          border-right: 0.05rem solid transparent;
          border-bottom: 0.08rem solid #17E8FF;
          position: absolute;
          right: -0.16rem;
          top: 0.03rem;
        }

        &::after {
          content: '';
          width: 0.1rem;
          height: 0.01rem;
          position: absolute;
          right: -0.16rem;
          top: 0.12rem;
          background-color: #17E8FF;
        }
      }

      .oper_top {
        position: absolute;
        width: 1.5rem;
        height: 1.5rem;
        background: url(@/assets/img/boxbj.png) no-repeat center;
        background-size: 100% 100%;
        bottom: 112%;
        z-index: 10;

        .close {
          img {
            position: absolute;
            right: 12%;
            top: 9%;
            cursor: pointer;
            width: 0.15rem;
            z-index: 10;
          }
        }

        .tabsAlert {
          height: 1.5rem;

          .el-tabs__item {
            color: #fff;
          }
        }
      }

      .oper_top_left {
        left: 13%;
      }

      .oper_top_right {
        right: 13%;
      }
    }
  }

  .operation_box {
    width: 100%;
    height: 2.4rem;
    // background: url(@/assets/img/box.png) no-repeat center;
    background-size: 100% 100%;
    background-position: center;
    background-repeat: no-repeat;
    display: flex;
    justify-content: space-between;
    position: relative;

    .box_left {
      width: 38%;
      padding: 0 0.26rem 0 0.4rem;
      display: flex;
      flex-direction: column;

      h2 {
        font-size: 0.22rem;
        font-family: "Pang";
        font-weight: 400;
        color: #FFFFFF;
        text-align: center;
        padding: 0.13rem 0 0.12rem 0;
        width: 100%;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }

      ul {
        width: 100%;
        height: 70%;
        overflow-y: scroll;

        li {
          display: flex;
          justify-content: space-between;
          margin-bottom: 0.01rem;

          span {
            text-align: center;
            color: #fff;
            font-size: 0.12rem;
            height: 0.27rem;
            line-height: 0.27rem;

            &:nth-child(1) {
              width: 33%;
              background-color: rgba(85, 136, 238, 0.4);
            }

            &:nth-child(2) {
              font-weight: bold;
              width: 66.6%;
              color: #17E8FF;
              background-color: rgba(85, 136, 238, 0.2);
              // overflow: hidden;
              // white-space: nowrap;
              // text-overflow: ellipsis;
            }
          }

          &:nth-child(1),
          &:nth-child(2) {
            span {
              &:nth-child(1) {
                letter-spacing: 0.2rem;
                padding-left: 0.2rem;
              }
            }
          }

          &:nth-child(8),
          &:nth-child(9) {
            span {
              &:nth-child(1) {
                height: auto;
              }

              &:nth-child(2) {
                height: auto !important;
              }
            }
          }
        }
      }
    }

    .box_line {
      width: 1%;

      img {
        width: 0.01rem;
        margin-top: 0.35rem;
        height: 1.7rem;
      }
    }

    .box_right {
      width: 60%;

      .oper_cont {
        width: 100%;

        .oper_btns {
          padding: 0.2rem 0.4rem 0 0;
        }

        .oper_echarts {
          width: 100%;
          height: 1.8rem;
        }
      }
    }

    .close {
      position: absolute;
      right: 2.6%;
      top: 6%;

      img {
        width: 0.18rem;
        display: flex;
        cursor: pointer;
      }
    }
  }
}

/*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/
::-webkit-scrollbar {
  // width: 2px;
  // height: 10px;
  display: none;
}
</style>